﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eTerm.MsSql.DbProvider;

namespace eTerm.Platform.Web
{
    public partial class CabinEditor : BaseEntityPage<PnrCabin>
    {
        protected void btnSave_Click(object sender, EventArgs e) {
            Save();
        }

        protected override void Save()
        {
            GetFromPage().Save();
        }

        protected override void InitEasyUI()
        {
            BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, btnSave.ClientID, string.Format(@"$('#{0}').linkbutton({{   
    iconCls: 'icon-save'  
}});  
", btnSave.ClientID), true);

        }


        protected override void InitPageLoad()
        {
            PnrCabin model = Get();
            if (model == null) return;
            hndID.Value = model.Id.ToString();
            tbAirWayCode.Text = model.AirWayCode;
            tbCabinCode.Text = model.CabinCode;
            tbDescription.Text = model.Description;
            tbDiscount.Text = model.Discount.Value.ToString("###");
        }


        protected override PnrCabin Get()
        {
            int ID = int.Parse(Request["ID"]);
            return PnrCabin.FirstOrDefault(l => l.Id == ID);
        }

        protected override PnrCabin GetFromPage()
        {
            PnrCabin model = null;
            if (string.IsNullOrEmpty(hndID.Value)) {
                model = new PnrCabin() { 
                 AirWayCode = tbAirWayCode.Text.Trim(),
                  CabinCode = tbCabinCode.Text.Trim(),
                   Description = tbDescription.Text.Trim(),
                    Discount = decimal.Parse(tbDiscount.Text.Trim())
                };
            }
            else {
                model = PnrCabin.FirstOrDefault(l => l.Id == int.Parse(hndID.Value));
                model.AirWayCode = tbAirWayCode.Text.Trim();
                model.CabinCode = tbCabinCode.Text.Trim();
                model.Description = tbDescription.Text.Trim();
                model.Discount = decimal.Parse(tbDiscount.Text.Trim());
            }

            return model;
        }
    }
}
